//This file is part of the store/retrieve project
//    (http://code.google.com/p/storeretrieve/).
//
//    Store/retrieve is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Store/retrieve is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with store/retrieve. If not, see <http://www.gnu.org/licenses/>.
//

package de.devcity.storeretrieve;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Tools {


	/* http://stackoverflow.com/questions/367626/how-do-i-fix-the-expression-of-type-list-needs-unchecked-conversion#comment2897919_2848268 */
	public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
		List<T> r = new ArrayList<T>(c.size());
		for(Object o: c)
			r.add(clazz.cast(o));
		return r;
	}

	public static Properties loadPropertiesByUser(ServletContext sc, String username) {
		Properties result = new Properties();
		File file = new File(sc.getRealPath("/WEB-INF/conf/user-" + username + ".properties"));
		if(file.exists()) {
			try{
				result.load(new FileInputStream(file));
			} catch(Throwable ex) {
				result = null;		
			} finally {}	
		} else {
			result = null;
		}
		return result;
	}

	//Reads a text element in a meta.xml's DOM
	public static String getFieldValueFromMetaDOM(Document doc, String key) {
		String result;

		try {
			XPath xpath = XPathFactory.newInstance().newXPath();
			XPathExpression expr = xpath
					.compile("/Container/" + ((String) key));
			NodeList nodes = (NodeList) (expr.evaluate(doc,
					XPathConstants.NODESET));
			if (nodes == null || nodes.getLength() == 0) {
				result = "";
			} else {
				result = nodes.item(0).getTextContent();
				if (result == null)
					result = "";
			}

		} catch (Throwable ex) {
			result = "";
			ex.printStackTrace();
		} finally {
		}
		return result;
	}

	public static String getIconLink(String icon, String href, String target) {
		StringBuilder o = new StringBuilder();
		o.append("<a href=\"");
		o.append(href);
		o.append("\" ");
		o.append("target=\"");
		o.append(target);
		o.append("\">");
		o.append("<img src=\"");
		o.append("resources/" + icon + ".png");
		o.append("\" /></a>");
		return o.toString();
	}

	public static String getLogoHtml() {
		return "<a href=\"http://code.google.com/p/storeretrieve/\">"
				+ "<img src=\"resources/logo-storeretrieve-small.png\" "
				+ "alt=\"store/retrieve\""
				+ "style=\"float:right;margin:0 5px 0 0;\" />" + "</a>";

	}
	
	public static String randomPathPart() {
		final String AB = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		final int LEN = 6;
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(LEN);
		for( int i = 0; i < LEN; i++ ) 
			sb.append( AB.charAt( rnd.nextInt(AB.length()) ) );
		return sb.toString();
	}

}
